/**
 * Echarts 折线图
 * @author 木人心
 * @version 1.0.1
 */
import { EChartsOption, LineSeriesOption } from 'echarts'
import { ObjectUtil } from '@utils/object-util'
import { CartesianSystemStyle, getCartesianSystemStyle } from './cartesianSystem'
import { ChartColor } from '../type'
import { EChartsUtil } from '../utils'

/**
 * 获取折线图基本样式
 */
function getLineStyleBase (): EChartsOption {
  const option = getCartesianSystemStyle();
  (option.xAxis as any).type = 'time'
  option.tooltip = {
    trigger: 'axis',
    axisPointer: { type: 'line' }
  }
  // (option.xAxis as any).boundaryGap = false
  return option
}

/**
 * 获取一条折线
 * @param config
 */
function getLineStyle (config: {
  color: string,
  name: string,
  unit?: string,
  area?: boolean,
  areaColor?: ChartColor
}) {
  const lineStyle = new LineStyle()
  lineStyle.addSeries(getLineSeries(config))
  lineStyle.setYAxis({ name: config.unit })
  lineStyle.setXAxis({ type: 'time', boundaryGap: false })
  return lineStyle
}

/**
 * 获取多条折线
 * @param config
 */
function getLinesStyle (config: {
  colors: string[],
  names: string[],
  unit?: string,
  area?: boolean,
  areaColors?: ChartColor[]
}) {
  if (!config.areaColors) config.areaColors = []
  const lineStyle = new LineStyle()
  lineStyle.setYAxis({ name: config.unit })
  lineStyle.setXAxis({ type: 'time', boundaryGap: false })
  for (let i = 0, len = config.names.length; i < len; i++) {
    lineStyle.addSeries(getLineSeries({
      color: config?.colors[i],
      name: config?.names[i],
      area: config.area,
      areaColor: config?.areaColors[i]
    }))
  }
  return lineStyle
}

/**
 * 获取一个折线系列
 * @param config
 * @param option
 */
function getLineSeries (config: {
  name: string,
  color?: string,
  area?: boolean,
  areaColor?: ChartColor
}, option?: LineSeriesOption): LineSeriesOption {
  const _option = { type: 'line', symbol: 'none', name: config.name, smooth: true } as any
  if (config.color) _option.itemStyle = { color: config.color }
  if ((config.area || ObjectUtil.isString(config.areaColor)) && config.color) {
    _option.areaStyle = {
      color: EChartsUtil.getLinearGradient(config.color, config.areaColor as string)
    }
  }
  if (option) ObjectUtil.assignDeep(_option, option)
  return _option
}

class LineStyle extends CartesianSystemStyle {
  currentOption: EChartsOption = getLineStyleBase()
}

export { getLineStyle, getLineSeries, getLinesStyle }
